package cn.ffcs.organ.dao;

import java.sql.ResultSet;
import java.util.List;

import org.echoice.spring.SpringContextUtil;
import org.hibernate.Session;

import cn.ffcs.base.dao.ExtBaseDAO;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.bpmpvc.services.bean.ConfigBean;
import cn.ffcs.organ.model.OrgRelaPerson;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.organ.model.Person;

/**
 * 组织人员关系DAO<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class OrgRelaPersonDAO<O extends OrgRelaPerson> extends ExtBaseDAO<O>
        implements IOrgRelaPersonDAO<O>
{

    public String getEntityName()
    {
        return new OrgRelaPerson().getClass().getName();
    }
    /*
     * 查询一个组织下的所有员工
     */
    public PageControl findByPageByOrg(OrgRelaPerson orgRelaPerson,int currPage, int pageSize){
        String hql=" from  OrgRelaPerson a where 1=1 ";
        if(orgRelaPerson.getOrganization()!=null&&orgRelaPerson.getOrganization().getId()!=null&&orgRelaPerson.getOrganization().getId().intValue()!=0)
        {
            hql+=" and a.organization.id="+orgRelaPerson.getOrganization().getId();
        }
        if(orgRelaPerson.getPerson()!=null&&orgRelaPerson.getPerson().getName()!=null&&!orgRelaPerson.getPerson().equals("")){
            hql+=" and a.person.name like '%"+orgRelaPerson.getPerson().getName()+"%'";
        }
        
        List list=super.getSession().createQuery("select count(*) "+hql).list();
        String result=list.get(0).toString();
        int count=Integer.parseInt(result);
        PageControl pageControl=new PageControl(count,currPage,pageSize);
        final int startRow = pageControl.getStart();
        List <OrgRelaPerson> ls =super.getSession().createQuery(hql).setFirstResult(pageSize*(currPage-1)).setMaxResults(pageSize).list();
        return new PageControl(ls, startRow, pageSize, currPage,
                pageControl.getTotalPage(), pageControl.getTotal());
    }
    
    /*
     * 查询一个组织下的所有员工
     */
    public List<Person> findByPageByOrg(String orgId){
        String hql=" from OrgRelaPerson a,Person b where b.end>sysdate and a.person=b.id and a.end>sysdate ";
        if(orgId!=null&&orgId!=null)
        {
            hql+=" and a.organization.id="+orgId;
        }
        return super.getSession().createQuery("select b "+hql).list();
    }

    /*
     * 查询一个员工所在的一级部门
     */
    public Organization findByPageByPersonId(String personId){
        java.sql.Connection conn = null;
        java.sql.Statement stmt = null;
        Session session = null;
        Organization organization=null;
        try
        {
            session = super.getSession();
            conn = session.connection();
            stmt = conn.createStatement();
            String sql="select b.id,b.name from (select a.* from t_organ_organization a start with a.id in(select t.organization from t_organ_org_rela_person t where t.person='"+personId+"') connect by prior a.superior=a.id) b where b.org_level<='2' order by org_level";
            ResultSet rs= stmt.executeQuery(sql);
            organization=new Organization();
            if(rs.next()){
                organization.setId(rs.getInt("id"));
                organization.setName(rs.getString("name"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            if (session != null)
            {
                releaseSession(session);
            }
        }
        return organization;
    }
    
}